package com.heima.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojo.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {

    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 登录接口
     * 二合一，用户登录也是有游客登录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login (LoginDto dto) {
        //判断是否是游客登录
        if (ObjectUtil.isEmpty (dto.getPhone ()) && ObjectUtil.isEmpty (dto.getPassword ())) {
            Map<String, Object> data = new HashMap<> ();
            Map<String, Object> user = new HashMap<> ();
            user.put ("id", "");
            user.put ("name", "");
            user.put ("phone","");
            data.put ("user", user);
            String token = AppJwtUtil.getToken (0L);
            data.put ("token", token);
            return ResponseResult.okResult (data);
        }
        System.out.println ("登录接口");
        System.out.println (dto.getPassword ());
        List<ApUser> apUsers = apUserMapper.selectList (Wrappers.<ApUser>lambdaQuery ()
                .eq (ApUser::getPhone, dto.getPhone ()));
        if (ObjectUtil.isEmpty (apUsers)) {
            return ResponseResult.errorResult (AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
        }
        for (ApUser apUser : apUsers) {
//            System.out.println (apUser.getPassword ());
//            System.out.println (dto.getPassword ());
            //数据库
            if(BCrypt.checkpw (dto.getPassword (), apUser.getPassword ())){
                Map<String, Object> data = new HashMap<> ();
                Map<String, Object> user = new HashMap<> ();
                user.put ("id", apUser.getId ());
                user.put ("name", apUser.getName ());
                user.put ("phone", apUser.getPhone ());
                data.put ("user", user);
                String token = AppJwtUtil.getToken (Long.valueOf (apUser.getId ()));
                data.put ("token", token);
                return ResponseResult.okResult (data);
            }
        }

        return ResponseResult.errorResult (AppHttpCodeEnum.LOGIN_PASSWORD_ERROR, "密码错误");
    }

    /**
     * 根据用户id获取用户实体
     *
     * @param userId
     * @return
     */
    @Override
    public ResponseResult<ApUser> findUserById (Integer userId) {
        ApUser apUser = getById (userId);
        return ResponseResult.okResult (apUser);
    }
}
